<?php

namespace app\common\model\ads;

use think\Model;

/**
 * 广告评论
 */
class Comment extends Model
{

    // 表名
    protected $name = 'self_comment';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;


   // 追加属性
    protected $append = [
        'createtime_text'
    ];

    
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }

    // public function getStatusList()
    // {
    //     return ['0' => "待领取", '1' => "已领取", "2" => "已退回"];
    // }

    // public function getStatusTextAttr($value, $data)
    // {
    //     $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
    //     $list = $this->getStatusList();
    //     return isset($list[$value]) ? $list[$value] : '';
    // }


    /**
     * 
     */
    public function getUserInfoAttr($value, $data){
        $value = isset($data['user_id']) ? $data['user_id'] : 0;
        return $value ? \app\common\model\User::getBaseInfoNosense($value) : new \stdClass();
    }

    /**
     * 
     */
    public function getZanNumAttr($value, $data){
        $value = isset($data['id']) ? $data['id'] : 0;
        return $value ? CommentZan::where('comment_id', $value)->count() : 0 ;
    }

    /**
     * 
     */
    public function getChildrenAttr($value, $data){
        $ad_id = isset($data['ad_id']) ? $data['ad_id'] : 0;
        $pid = isset($data['id']) ? $data['id'] : 0;
        return collection(self::where([
            'ad_id' => $ad_id,
            'pid' => $pid,
        ])->select())->each(function($item){
            $item->user_info = $item->user_info;
        });
    }

    

}
